UITimer = {}
UITimerFunc = {}
UITimerFunc.__index = UITimerFunc
setmetatable(UITimer, UITimerFunc)
UITimer.timerID = nil
UITimer.uiTimers = {}
UITimer.getSysTime = function()
  return TimeManager.deltaTime + os.time()
end

UITimer.addUITimer = function(uitimer)
  UITimer.uiTimers[uitimer.name] = uitimer
  if UITimer.timerID==nil then
    print("UITimer.addUITimer begin timer")
    UITimer.timerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(UITimer.onTimer, 1, false) 
  end
end
UITimer.removeUITimer = function(uitimer)
  for k, v in pairs(UITimer.uiTimers) do
    if v==uitimer then
      UITimer.uiTimers[k] = nil
    end
  end
end
UITimer.getUITimer = function(name, handler, labelPos, buttonPos, hideButton)
  if UITimer.uiTimers[name]~=nil then
    return UITimer.uiTimers[name]
  end
  local tmp = CCUserDefault:sharedUserDefault():getStringForKey(name)
  print("UITimer.getUITimer", name, tmp)
  local lt=0
  if tmp~=nil and tmp~="" then
    local res = cjson.decode(tmp)
    local dt = UITimer.getSysTime() - res.beginTime
    lt = res.duration - dt
    if lt<0 then lt=0;end
  end
  if lt <= 0 then return nil end
  local uitimer = UITimer.createUITimer(name, lt, handler, labelPos, buttonPos, hideButton)
  UITimer.addUITimer(uitimer)
  return uitimer
end

UITimer.onTimer = function()
  local stopTimer = true
  for k,v in pairs(UITimer.uiTimers) do
    stopTimer = false
    v:update()
  end
  if stopTimer==true then
    print("UITimer.addUITimer stop timer")
    CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(UITimer.timerID)
    UITimer.timerID = nil
  end
end
UITimer.createUITimer = function(name, duration, handler, labelPos, buttonPos, hideButton)
  local l = {}
  setmetatable(l, UITimerFunc)
  l.name = name
  l.duration = duration
  l.handler = handler
  l.view = CCLayer:create()
  
  l.label = CCLabelTTF:create(string.format("%s",formatSecond(duration)), "Helvetica", 28) 
  l.label:setAnchorPoint(ccp(1,0))
  --l.label:setPosition(ccp(0, 10))
  l.label:setPosition(labelPos)
  l.view:addChild(l.label)

  --local size = l.label:getContentSize()
  if hideButton==nil or hideButton==false then
    l.button = CCButton:createWithFileName("onJiaSuButton", "btn_green_normal.png", "btn_green_highlight.png", "btn_disabled.png", "加速") 
    l.button:setAnchorPoint(ccp(0,0))
    l.button:setPosition(buttonPos)
    setObjectExtTable(l.button, l)
    l.view:addChild(l.button)
  end
  UITimer.addUITimer(l)
  local tmp = string.format('{"beginTime":%d, "duration":%d}', UITimer.getSysTime(), duration)
  CCUserDefault:sharedUserDefault():setStringForKey(name, tmp)
  CCUserDefault:sharedUserDefault():flush()

  return l
end

UITimerFunc.update = function(self)
  --[[print("UITimerFunc.update", self.view)
  print("UITimerFunc.update", self.view, self.view:getParent())
  if self.view:getParent()==nil then
    print("UITimerFunc.update remove timer")
    UITimer.removeUITimer(self)  
    return
  end]]
  self.duration = self.duration - 1
  if self.duration<=0 then
    self.label:setString(string.format("%s",formatSecond(self.duration)))
    self:endTimer(true)
  else
    self.label:setString(string.format("%s",formatSecond(self.duration)))
  end
end
UITimerFunc.checkCall = function(self, action, pars)
  if self.handler~=nil then
    local metaFunc = getmetatable(self.handler)
    if metaFunc[action]~=nil then
      metaFunc[action](self.handler, self, pars)
    end
  end
end
UITimerFunc.endTimer = function(self, isCallback)
  self.duration = 0
  UITimer.removeUITimer(self)  
  CCUserDefault:sharedUserDefault():setStringForKey(self.name, "")
  CCUserDefault:sharedUserDefault():flush()
  if isCallback==true then
    self:checkCall("onTimerEnd")
  end
  --self.view:removeChild(self.label, true)
  --self.view:removeChild(self.button, true)
end
UITimerFunc.onJiaSuButton = function(self, node, event)
  self:checkCall("onJiaSuButton", {leftTime=self.duration})
  --setTouchEnabledAll(self.view:getParent(), false)
  --local l = JiaSuDialog:createExt("test", self.duration, self.view:getParent(), {target=self, action="onJiaSuComplete"})
  --local l = JiaSuSampleDialog:create(self.duration, self)
  --self.view:getParent():addChild(l.node)
end
--[[
UITimerFunc.onJiaSuComplete = function(self)
  self.duration = 0
  self:endTimer()
end
]]
